package com.wenhui.schoolyardinfo.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.firstpage.bean.MineAlbumBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/6 09:37
 * introduce :
 */

public class MineAlbumAdapter extends RecyclerView.Adapter {

    private static final String TAG = "MineAlbumAdapter";
    private Context context;
    private List<MineAlbumBean> mineAlbumBeanList = new ArrayList<>();
    private static final int HEADER_VIEW_TYPE = 1;
    private static final int CONTENT_VIEW_TYPE = 2;

    public MineAlbumAdapter(Context context) {
        this.context = context;
    }

    public MineAlbumAdapter(Context context, List<MineAlbumBean> mineAlbumBeanList) {
        this.context = context;
        this.mineAlbumBeanList = mineAlbumBeanList;
    }

    public void clearData() {
        mineAlbumBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<MineAlbumBean> mineAlbumBeanList) {
        this.mineAlbumBeanList.addAll(mineAlbumBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_mine_album_header, parent, false);
                return new HeaderHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_mine_album_content, parent, false);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            if (mineAlbumBeanList.get(position - 1).getPhotoBeanList() != null && mineAlbumBeanList.get(position - 1).getPhotoBeanList().size() > 0) {
                Glide.with(context).load(mineAlbumBeanList.get(position - 1).getPhotoBeanList().get(0).getPhotoUrl()).into(contentHolder.imvPhotoAlbum);
            }

            contentHolder.tevPhotoAlbumName.setText(mineAlbumBeanList.get(position - 1).getAlbumName());
            contentHolder.tevPhotoAlbumNum.setText(mineAlbumBeanList.get(position - 1).getPhotoNum() + "张");
            contentHolder.tevPhotoAlbumJurisdiction.setText(mineAlbumBeanList.get(position - 1).getAlbumJurisdiction());

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mineAlbumBeanList != null && mineAlbumBeanList.size() > 0) {//如果mineAlbumBeanList有数据
            if (position == 0) {
                return HEADER_VIEW_TYPE;
            } else {
                return CONTENT_VIEW_TYPE;
            }
        } else {//如果mineAlbumBeanList无数据
            return HEADER_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        if (mineAlbumBeanList != null && mineAlbumBeanList.size() > 0) {
            return mineAlbumBeanList.size() + 1;
        } else {
            return 1;
        }
    }

    protected class HeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_new_album)
        ImageView imvNewAlbum;
        @BindView(R.id.tev_new_album)
        TextView tevNewAlbum;
        @BindView(R.id.layout_new_album)
        RelativeLayout layoutNewAlbum;

        private HeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutNewAlbum.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_new_album:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_photo_album)
        ImageView imvPhotoAlbum;
        @BindView(R.id.tev_photo_album_name)
        TextView tevPhotoAlbumName;
        @BindView(R.id.tev_photo_album_num)
        TextView tevPhotoAlbumNum;
        @BindView(R.id.tev_photo_album_jurisdiction)
        TextView tevPhotoAlbumJurisdiction;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            imvPhotoAlbum.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.imv_photo_album:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }

}
